package com.qianji.service.impl;

import cn.hutool.core.date.DateTime;
import com.qianji.mapper.ActiveMapper;
import com.qianji.pojo.ActiveDto;
import com.qianji.pojo.TurntableDto;
import com.qianji.service.ActiveService;
import com.qianji.utils.Result;
import com.qianji.vo.ActiveAddVO;
import com.qianji.vo.ActiveUpdateVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by 阳家桂 on 2021/3/12 15:37
 */
@Service
public class ActiveServiceImpl implements ActiveService {


    @Autowired
    private ActiveMapper activeMapper;

    /**
     * 添加活动
     * @param activeAddVO
     * @return
     */
    @Override
    public Integer addActive(ActiveAddVO activeAddVO) {
        //设置创建时间为当前时间
        activeAddVO.setCreateTime(new Date());

        return activeMapper.addActive(activeAddVO);
    }


    /**
     * 根据id删除活动，只将状态改变，不做真实删除
     * @param id
     * @return
     */
    @Override
    public Integer deletActiveById(String id) {

        ActiveDto activeDto = selectStatus(Long.valueOf(id));
        if (activeDto.getStatus()==1){
            return 0;
        }

        Date updateTime = new Date();

        return activeMapper.deletActiveById(id,updateTime);
    }



    /**
     * 开启或关闭活动
     * @param
     * @return
     */
    @Override
    public Integer onOff(String id) {
        Long lId = Long.valueOf(id);
        ActiveDto activeDto = selectStatus(lId);
        if (activeDto.getStatus() == 0){
            activeDto.setStatus(1);
            activeDto.setId(lId);
            activeDto.setUpdateTime(new DateTime());
        }else if (activeDto.getStatus() == 1){
            activeDto.setStatus(0);
            activeDto.setId(lId);
            activeDto.setUpdateTime(new DateTime());
        }
        return activeMapper.onoff(activeDto);
    }

    /**
     * 查询所有活动
     * @return
     */
    @Override
    public Result selectAll(ActiveDto activeDto) {

        //获取当前页数
        activeDto.setPage((activeDto.getPage()-1)* activeDto.getLimit());

        List<ActiveDto> activeDtoList = activeMapper.selectAll(activeDto);
        //总数据条数
        Long count = activeMapper.count(activeDto);
        return Result.success(activeDtoList,count);
    }


    /**
     * 修改活动
     * @param activeUpdateVO
     * @return
     */
    @Override
    public Integer updateActive(ActiveUpdateVO activeUpdateVO) {
        activeUpdateVO.setUpdateTime(new Date());
        return activeMapper.updateActive(activeUpdateVO);
    }

    @Override
    public ActiveDto selectById(Long id) {

        return activeMapper.selectById(id);
    }

    @Override
    public ActiveDto smashEggs(Long id) {

        return activeMapper.smashEggs(id);
    }

    @Override
    public TurntableDto turntable(Long id) {

        return activeMapper.turntable(id);
    }

    @Override
    public String findTurntable(Long id) {

        return activeMapper.findTurntable(id);
    }

    /**
     * 获取活动状态
     * @param id
     * @return
     */
    private ActiveDto selectStatus(Long id){

       return activeMapper.selectStatus(id);
    }


}
